/*
	Funcion que sirve para Dado un XMl de la forma <registros>..</registros>
	lo ordena y lo filtra por los campos dados
	Filtra por un solo campo(sCampoFiltro) 
	Ordena por un solo campo (sCampo) con un orden (sOrden)
*/
function FiltrarOrdenar(oXML,sCampoFiltro,sValor,sCampo,sOrden)
{
	var sXsl
	var oXsl = new ActiveXObject("Microsoft.XMLDOM")
	
try{
	//si no tiene registros se va
	if (oXML.childNodes(0).childNodes.length == 0)
		return "<registros></registros>"
	
	sXsl = "<registros>"
	sXsl = sXsl + "<xsl:for-each order-by='" + sOrden + sCampo+"' select='registro' xmlns:xsl='http://www.w3.org/TR/WD-xsl' >"

	sXsl = sXsl + " <xsl:if test="+String.fromCharCode(34)+sCampoFiltro+"[.$ige$'"+ sValor +"' and .$ilt$'"+ FiltroMaximo(sValor) +"']"+String.fromCharCode(34)+"> "
	sXsl = sXsl + "<registro>"

		for (var j=0; j < oXML.childNodes(0).childNodes(0).childNodes.length ;j++)
		 {
			sXsl = sXsl + "<" + oXML.childNodes(0).childNodes(0).childNodes(j).nodeName + "><xsl:value-of select='" + oXML.childNodes(0).childNodes(0).childNodes(j).nodeName + "' /> </" + oXML.childNodes(0).childNodes(0).childNodes(j).nodeName + ">"		
		 }	

	sXsl = sXsl + "</registro>"
	sXsl = sXsl + "</xsl:if>"
	sXsl = sXsl + "</xsl:for-each>"
	sXsl = sXsl + "</registros>"

	oXsl.loadXML(sXsl)
	//alert(oXML.xml)
	//alert(oXsl.xml)
	
	return oXML.documentElement.transformNode(oXsl)
	}
catch(e)
{
	alert("Error al filtrar los datos: \n" + e.description)
	return "<registros></registros>"
}	

}

function FiltroMaximo(sCadena)
{
  var iLongCadena = sCadena.length
  var sCadenaAux = sCadena.substr(0,(iLongCadena -1))
   
  sCadenaAux = sCadenaAux + String.fromCharCode((sCadena.charCodeAt(iLongCadena-1)+1))

  return sCadenaAux


}
function GenerarXSL(sXML,sCampo,sOrden)
{
var sXsl
var nomas = 0
sXsl = "<registros>"
	sXsl = sXsl + "<xsl:for-each order-by='" + sOrden + sCampo+"' select='registro' xmlns:xsl='http://www.w3.org/TR/WD-xsl'>"
	sXsl = sXsl + "<registro>"

	for (var j=0; j < sXML.childNodes(0).childNodes(0).childNodes.length ;j++)
	 {
		
		
		if (sXML.childNodes(0).childNodes(0).childNodes(j).childNodes.length > 1 )
		  {
			if ( nomas == 0){	
				
				sXsl = sXsl + "<xsl:for-each select='" +sXML.childNodes(0).childNodes(0).childNodes(j).nodeName + "' >"
				sXsl = sXsl + "<" + sXML.childNodes(0).childNodes(0).childNodes(j).nodeName+ ">"
				
				for (var k=0; k < (sXML.childNodes(0).childNodes(0).childNodes(j).childNodes.length ) ;k++)
				 {
					sXsl = sXsl + "<" + sXML.childNodes(0).childNodes(0).childNodes(j).childNodes(k).nodeName + "><xsl:value-of select='" + sXML.childNodes(0).childNodes(0).childNodes(j).childNodes(k).nodeName + "' /></" + sXML.childNodes(0).childNodes(0).childNodes(j).childNodes(k).nodeName + ">"
				 }	
				
				sXsl = sXsl + "</" + sXML.childNodes(0).childNodes(0).childNodes(j).nodeName+ ">"
				sXsl = sXsl + "</xsl:for-each>"
				nomas = 1	
			 }					
		  }else{
			sXsl = sXsl + "<" + sXML.childNodes(0).childNodes(0).childNodes(j).nodeName + "><xsl:value-of select='" + sXML.childNodes(0).childNodes(0).childNodes(j).nodeName + "' /> </" + sXML.childNodes(0).childNodes(0).childNodes(j).nodeName + ">"
			//alert(sXsl)
		  } 	
	 }	

	sXsl = sXsl + "</registro>"
	sXsl = sXsl + "</xsl:for-each>"
	sXsl = sXsl + "</registros>"
	//alert(sXsl)


//window.document.all.txtPageSize.value = sXsl
return sXsl


}

//Xsl para ordenar un xml cuando tenemos un corte de control.

function GenerarXSLCorte(sXML,sCampo,sOrden){
var sXsl
var nomas = 0
sXsl = "<registros>"
	
	for (var j=0; j < sXML.childNodes(0).childNodes.length ;j++){
		
		if (sXML.childNodes(0).childNodes(j).childNodes.length > 1 ){
				if ( nomas == 0){	
																				
					sXsl = sXsl + "<xsl:for-each select='registro' xmlns:xsl='http://www.w3.org/TR/WD-xsl'>"
					sXsl = sXsl + "<registro>"
					
					//sXsl = sXsl + "<" + sXML.childNodes(0).childNodes(0).childNodes(j).nodeName + "><xsl:value-of select='" + sXML.childNodes(0).childNodes(0).childNodes(j).nodeName + "' /></" + sXML.childNodes(0).childNodes(0).childNodes(j).nodeName + ">"
					sXsl = sXsl + "<" + sXML.childNodes(0).childNodes(j).childNodes(0).nodeName + "><xsl:value-of select='" + sXML.childNodes(0).childNodes(j).childNodes(0).nodeName + "' /></" + sXML.childNodes(0).childNodes(j).childNodes(0).nodeName + ">"
						
					sXsl = sXsl + "<xsl:for-each order-by='" + sOrden + sCampo+"' select='r'>"
					sXsl = sXsl + "<" + sXML.childNodes(0).childNodes(0).childNodes(j+1).nodeName + ">"						
					nomas = 1
				 }	
										
				iCant = sXML.childNodes(0).childNodes(0).childNodes(j).childNodes.length
				
				if(iCant == 1){
					iCant++
					j++
				}
						 
				if (iCant > 1){ 
					
					for (var k=0; k < sXML.childNodes(0).childNodes(0).childNodes(j).childNodes.length ;k++){
						
						sXsl = sXsl + "<" + sXML.childNodes(0).childNodes(0).childNodes(j).childNodes(k).nodeName + "><xsl:value-of select='" + sXML.childNodes(0).childNodes(0).childNodes(j).childNodes(k).nodeName + "' /></" + sXML.childNodes(0).childNodes(0).childNodes(j).childNodes(k).nodeName + ">"
						
					}	
						
					sXsl = sXsl + "</" + sXML.childNodes(0).childNodes(0).childNodes(j).nodeName+ ">"
					sXsl = sXsl + "</xsl:for-each>"
				}
				
				
				
			}
		
	 }	

	
	sXsl = sXsl + "</registro>"
	sXsl = sXsl + "</xsl:for-each>"
	sXsl = sXsl + "</registros>"
	//alert(sXsl)
	//window.document.all.txtPageSize.value = sXsl
return sXsl


}
